package com.cssw.fyzb.tps.service.im;

import com.cssw.fyzb.tps.service.im.common.ImHttpComm;
import com.cssw.fyzb.tps.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author Jian Qin.
 */
@Service
public class GroupServiceImpl implements GroupService{
    @Autowired
    private ImHttpComm imHttpComm;

    @Override
    public ImHttpComm.ImResult createGroup(String owner_account, String groupId,String type, String name, String introduction,
                                           String notification, String faceUrl, Integer maxMemberCount) {
        Map<String, Object> params = new HashMap<>();
        params.put("Owner_account", owner_account);
        params.put("GroupId", groupId);
        params.put("Type", type);
        params.put("Name", name);
        params.put("Introduction", introduction);
        params.put("Notification", notification);
        params.put("FaceUrl", faceUrl);
        if(maxMemberCount != null){
            params.put("MaxMemberCount", maxMemberCount);
        }

        return imHttpComm.post("/group_open_http_svc/create_group", params);
    }

    @Override
    public ImHttpComm.ImResult getGroupList(Integer limit, Integer next, String groupType) {
        Map<String, Object> params = new HashMap<>();
        if(limit != null && next != null){
            params.put("Limit", limit);
            params.put("Next", next);
        }

        if(!StringUtil.isBlank(groupType)){
            params.put("GroupType", groupType);
        }

        return imHttpComm.post("/group_open_http_svc/get_appid_group_list", params);
    }

    @Override
    public ImHttpComm.ImResult getGroupInfo(String[] groupIdLists) {
        Map<String, Object> params = new HashMap<>();
        params.put("GroupIdList", groupIdLists);

        return imHttpComm.post("/group_open_http_svc/get_group_info", params);
    }

    @Override
    public ImHttpComm.ImResult modifyGroup(String groupId, String name, String introduction, String notification, String faceUrl, Integer maxMemberCount, String shutUpAllMember) {
        Map<String, Object> params = new HashMap<>();
        params.put("GroupId", groupId);
        if(!StringUtil.isBlank(shutUpAllMember)){
            params.put("ShutUpAllMember", shutUpAllMember);
        }

        if(!StringUtil.isBlank(name)){
            params.put("Name", name);
        }

        if(!StringUtil.isBlank(introduction)){
            params.put("Introduction", introduction);
        }

        if(!StringUtil.isBlank(notification)){
            params.put("Notification", notification);
        }

        if(!StringUtil.isBlank(faceUrl)){
            params.put("FaceUrl", faceUrl);
        }

        if(maxMemberCount != null){
            params.put("MaxMemberCount", maxMemberCount);
        }

        return imHttpComm.post("/group_open_http_svc/modify_group_base_info", params);
    }

    @Override
    public ImHttpComm.ImResult setGroupManager(String groupId, String member_account) {
        Map<String, Object> params = new HashMap<>();
        params.put("GroupId", groupId);
        params.put("Member_account", member_account);
        params.put("Role", "Admin");

        return imHttpComm.post("/group_open_http_svc/modify_group_member_info", params);
    }

    @Override
    public ImHttpComm.ImResult cancelGroupManager(String groupId, String member_account) {
        Map<String, Object> params = new HashMap<>();
        params.put("GroupId", groupId);
        params.put("Member_account", member_account);
        params.put("Role", "Member");

        return imHttpComm.post("/group_open_http_svc/modify_group_member_info", params);
    }

    @Override
    public ImHttpComm.ImResult destroyGroup(String groupId) {
        Map<String, Object> params = new HashMap<>();
        params.put("GroupId", groupId);

        return imHttpComm.post("/group_open_http_svc/destroy_group", params);
    }

    @Override
    public ImHttpComm.ImResult forbidSendMsg(String groupId, String[] members_accounts, Integer hutUpTime) {
        Map<String, Object> params = new HashMap<>();
        params.put("GroupId", groupId);
        params.put("Members_Account", members_accounts);
        params.put("HutUpTime", hutUpTime);

        return imHttpComm.post("/group_open_http_svc/forbid_send_msg", params);
    }

    @Override
    public ImHttpComm.ImResult getGroupShutted(String groupId) {
        Map<String, Object> params = new HashMap<>();
        params.put("GroupId", groupId);

        return imHttpComm.post("/group_open_http_svc/get_group_shutted_uin", params);
    }

    @Override
    public ImHttpComm.ImResult sendGroupSystemNotification(String groupId, String content) {
        Map<String, Object> params = new HashMap<>();
        params.put("GroupId", groupId);
        params.put("Content", content);

        return imHttpComm.post("/group_open_http_svc/send_group_system_notification", params);
    }

    @Override
    public ImHttpComm.ImResult getGroupOnlineNum(String groupId) {
        Map<String, Object> params = new HashMap<>();
        params.put("GroupId", groupId);

        return imHttpComm.post("/group_open_http_svc/get_online_member_num", params);
    }
}
